package com.ant.storage.controller;


import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.DepDto;
import com.ant.storage.dto.PickingDto;
import com.ant.storage.pojo.Picking;
import com.ant.storage.pojo.PickingDetails;
import com.ant.storage.pojo.UserInfo;
import com.ant.storage.service.IDepartmentService;
import com.ant.storage.service.IPickingDetailsService;
import com.ant.storage.service.IPickingService;
import com.ant.storage.utils.UserInfoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-10-28
 */
@RestController
@RequestMapping("/picking")
@Api(tags = "领料接口")
public class PickingController {

    @Autowired
    private IPickingService pickingService;

    @Autowired
    private IPickingDetailsService pickingDetailsService;

    @Autowired
    public ServletContext servletContext;

    @ApiOperation(value = "获取领料数据列表")
    @GetMapping("/getPickingList")
    public ResultPage getPickingList(PickingDto dto) {
        return ResultPage.success("获取成功", pickingService.querylist(dto));
    }

    @ApiOperation(value = "获取领料数据列表")
    @GetMapping("/getPickingList2")
    public ResultPage getPickingList2(PickingDto dto) {
        return ResultPage.success("获取成功", pickingService.querylist2(dto));
    }

    @ApiOperation(value = "获取领料数据列表")
    @GetMapping("/getPickingDepList")
    public ResultPage getPickingDepList(PickingDto dto) {
        return ResultPage.success("获取成功", pickingService.getPickingDepList(dto));
    }

    @ApiOperation(value = "获取领料数据列表")
    @GetMapping("/getPickingByDepIdList")
    public ResultVo getPickingByDepIdList(PickingDto dto) {
        return ResultVo.success("获取成功", pickingService.getPickingByDepIdList(dto));
    }

    @ApiOperation(value = "保存领料单")
    @PostMapping("/savePicking")
    public ResultVo savePicking(@RequestBody Picking picking) throws IOException {
        return pickingService.savePicking(picking);
    }

    @ApiOperation(value = "修改领料单")
    @PostMapping("/pickingComplete")
    public ResultVo pickingComplete(@RequestBody  Picking picking) {
      return pickingService.pickingComplete(picking);
    }

    @ApiOperation(value = "通过id获取领料单详情")
    @GetMapping("/getPickingById/{id}")
    public ResultVo getPickingById(@PathVariable(value = "id", required = false) Integer id) {
        if (id != null) {
            return ResultVo.success("操作成功", pickingService.getPickingByID(id));
        } else {
            return ResultVo.fail("请输入ID号");
        }
    }

    @ApiOperation(value = "统计部门领料")
    @GetMapping("/findPickingCensus")
    public ResultVo findPickingCensus(DepDto dto) {
        return ResultVo.success("操作成功", pickingService.findPickingCensus(dto));
    }

    @ApiOperation(value = "统计部门领料")
    @GetMapping("/findPickingDepId/{id}")
    public ResultVo findPickingDepId(@PathVariable("id") Integer id) {
        return ResultVo.success("操作成功", pickingService.list(new QueryWrapper<Picking>().eq("dep_id",id)));
    }

    @ApiOperation(value = "删除")
    @PutMapping("/del/{id}")
    public ResultVo delPickingById(@PathVariable("id") Integer id) {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        pickingService.update(new UpdateWrapper<Picking>()
                .set("is_delete", 0)
                .set("creation_by",userInfo.getNameId())
                .set("delivery_time",new Date())
                .eq("id", id));
        return ResultVo.success("操作成功");
    }

    @ApiOperation(value = "删除")
    @PutMapping("/delPickingDetails/{id}")
    public ResultVo delPickingDetails(@PathVariable("id") Integer id) {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        if(id==null){
            return ResultVo.fail("id不能为空");
        }else {
            pickingDetailsService.update(new UpdateWrapper<PickingDetails>()
                    .set("is_delete", 0)
                    .set("creation_by", userInfo.getNameId())
                    .set("creation_time", new Date())
                    .eq("id", id));
        }
        return ResultVo.success("操作成功");
    }


    @ApiOperation(value = "修改")
    @PostMapping("/PickingDetailsEdit")
    public ResultVo PickingDetailsEdit(@RequestBody PickingDetails details) {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        if(details==null){
            return ResultVo.fail("id不能为空");
        }else {
            details.setCreationBy(userInfo.getNameId().toString());
            details.setCreationTime(new Date());
            pickingDetailsService.updateById(details);
            return ResultVo.success("操作成功");
        }
    }
}
